<?php
require_once("../resources/commons.php");
require_once("../data/User.php");
require_once("../data/Activity.php");
require_once("admin_commons.php");
checkAdminRights();

/**
 * Export the activties as a CSV file
 * @param string $p_exportFilePath
 * @return boolean true of the export was successful
 */
function exportActivitiesFile($p_exportFilePath){
	$l_handle = fopen($p_exportFilePath, "w");

	$l_records=array();
	$l_activities=Activity::getAll();
	foreach($l_activities as $l_activity){
		$l_responsibleUser=$l_activity->getResponsibleUser_Id_Ref();
		if(isset($l_responsibleUser)){
			$l_responsibleUserEmail=strtolower($l_responsibleUser->getEmail());
		}
		$l_records[]=array(
		$l_activity->getId(),
		$l_activity->getTitle(),
		$l_activity->getDescription(),
		$l_activity->getIsArchived(),
		$l_activity->getActivitytype_id(),
		$l_responsibleUserEmail
		);
	}

	if($l_handle){
		foreach($l_records as $l_record){
			fputcsv($l_handle, $l_record);
		}
		fclose($l_handle);
	}else{
		return false;
	}
	return true;
}

/**
 * Import the activties CSV file.<br/>
 * The CSV file will be used to create new activities or update existing ones if the ID is provided.
 * @param string $p_importFilePath
 * @return boolean true of the import was successful
 */
function importActivitiesFile($p_importFilePath){
	$l_handle = fopen($p_importFilePath, "r");
	$l_records=array();
	if($l_handle){
		while (!feof($l_handle)) {
			$l_records[]=fgetcsv($l_handle);
		}
		fclose($l_handle);
	}else{
		return false;
	}

	foreach($l_records as $l_record){
		if(count($l_record)==6){
			//The order of the fields is important (ID,Title,Description,IsArchived,Activitytype_Id,ResponsibleUserEmail)
			$l_activityId=$l_record[0];
			$l_activity=new Activity();
			$l_activity->setTitle($l_record[1]);
			$l_activity->setDescription($l_record[2]);
			$l_activity->setIsArchived($l_record[3]);
			$l_activity->setActivityType_id($l_record[4]);
			//Get the reponsible user email
			if(trim($l_record[5])!=""){
				$l_records=User::getFromCustomCriteria("WHERE Email=:Email", array("Email" => $l_record[5]));
				if(count($l_records)==1){
					$l_responsibleUser=array_shift($l_records);
					$l_activity->setResponsibleUser_id($l_responsibleUser->getId());
				}
			}
			if(trim($l_activityId)!=""){
				//Update
				$l_activity->setId($l_activityId);
			}
			$l_activity->save();
		}
	}
	return true;
}

displayTop("Import/Export activities",null,null);
$l_action=$_REQUEST["action"];
$l_filePath=$_REQUEST["path"];
if(isset($l_action) && isset($l_filePath)){
	$l_result=false;
	//Overwrite is not allowed
	if($l_action=="export" && !is_file($l_filePath)){
		$l_result=exportActivitiesFile($l_filePath);
	}
	if($l_action=="import" && is_file($l_filePath)){
		$l_result=importActivitiesFile($l_filePath);
	}
	if($l_result){
		echo("<h1>Operation completed successfully !</h1>");
	}else{
		echo("<h1>Failure, unable to complete !</h1>");
	}
}else{
	?>
<h1>Import/Export activities</h1>
<p><i>
This page allow to perform bulk import/export of activities.<br/>
The CSV files (Comma Separated Values) have the following structure:
<ul>
<li><b>ID: </b>(optional) the ID of the activity.<br/>
If an ID is provided in an import file, this means that the corresponding record in the database will be updated.<br/>
Othewise this will lead to the creation of a new Activity in the database (the ID will be automatically
computed).
</li>
<li><b>Title: </b>(mandatory) the title for this activity</li>
<li><b>Description: </b>(optional) a description (HTML formal allowed)</li>
<li><b>IsArchived: </b>(mandatory) 0 if the activity is "active", 1 otherwise</li>
<li><b>Activitytype_Id: </b>(mandatory) the value will depending on what you configured during 
the setup (0,1,2, etc. depending on the number of activity types in the order it was provided during the
setup)</li>
<li><b>ResponsibleUserEmail: </b>(optional) the e-mail address of the responsible user</li>
</ul>
</i></p>
<br/>
<u>Example or file row:</u>
<pre>
W-489ae4261d3204.35190005-556619,"Project X","This is project B description",0,0,james.bond@mi6.co.uk
</pre>
<form method="post" action="import_export_activities.php">
<p><b>File location:</b> <input type="text" name="path"/></p>
<p><b>Action type</b><br/>
Import <input type="radio" name="action" value="import"/><br/>
Export <input type="radio" name="action" value="export"/>
</p>
<input type="submit"/>
</form>
	
	<?php
}
?>

<?php
displayBottom();
?>